package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.commom.R;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.service.YddArticleService;
import com.itheima.ydd.service.YddUserDataService;
import com.itheima.ydd.service.YddUserService;
import com.itheima.ydd.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * 会员管理
 */
@RestController
@RequestMapping("/ydd_user")
@Slf4j
public class YddUserController {

    @Autowired
    private YddUserService yddUserService;
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private YddUserDataService yddUserDataService;

    /**
     * 用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R getUserById(Integer id) {
        log.info("根据id查询用户信息");
        YddUser yddUser = yddUserService.getById(id);
        if (yddUser != null) {
            yddUser.setPassword("");
            return R.success("", yddUser);
        } else {
            return R.error("没有查询到对应的用户信息");
        }
    }

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param sex
     * @param username
     * @param mobile
     * @return
     */
    @GetMapping("/list")
    public R page(Integer page, Integer limit, Integer sex, String username, String mobile) {
        log.info("page={},limit={},sex={},username={},mobile={}", page, limit, sex, username, mobile);
        Page<YddUser> pageInfo = new Page<>(page, limit);

        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(mobile), YddUser::getMobile, mobile);
        queryWrapper.like(StringUtils.isNotEmpty(username), YddUser::getUsername, username);
        queryWrapper.eq(sex != null, YddUser::getSex, sex);

        yddUserService.page(pageInfo, queryWrapper);
        List<YddUser> records = pageInfo.getRecords();

        PageUtils<YddUser> pageUtils = new PageUtils<>(pageInfo, records);
        return R.success(" ", pageUtils);
    }

    /**
     * 添加用户
     *
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody YddUser yddUser) {
        log.info("yddUser:{}", yddUser.toString());
        yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes(StandardCharsets.UTF_8)));
        yddUserService.save(yddUser);
        return R.success("添加成功", yddUser);
    }

    /**
     * 根据ID进行用户删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R deleteById(Integer id) {
        YddUser yddUser = yddUserService.getById(id);
        Integer id1 = yddUser.getId();//用户的id
        LambdaQueryWrapper<YddUserData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUserData::getUserId, id1);
        yddUserDataService.remove(queryWrapper);//查点赞表,有则直接删除

        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddArticleLambdaQueryWrapper.eq(YddArticle::getUserId, id1);
        yddArticleService.remove(yddArticleLambdaQueryWrapper);//删除文章表信息

        yddUserService.removeById(id);//删除用户信息
        return R.success("删除成功", id);
    }

    /**
     * 根据ID进行修改
     *
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddUser yddUser) {
        if (yddUser.getPassword().equals("")) {
            YddUser byId = yddUserService.getById(yddUser.getId());
            String password = byId.getPassword();
            yddUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)));
        }
        yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes(StandardCharsets.UTF_8)));
        yddUserService.updateById(yddUser);
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddArticle::getUserId, yddUser.getId());
        List<YddArticle> list = yddArticleService.list(queryWrapper);
        if (list != null) {
            for (YddArticle yddArticle : list) {
                yddArticle.setAuthor(yddUser.getUsername());
                yddArticleService.updateById(yddArticle);
            }
        }
        return R.success("修改成功", yddUser);
    }
}
